# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: search_video_id_name.py
Author: kij
Create Date: 2021/2/12
-------------------------------------------------
"""
from module_two.day15.homework import config


# 按id搜索
def search_by_id(video_id):
	print("您要搜索id为{}的新闻".format(video_id))
	name = ''
	with open(config.VIDEO_PATH, mode='rt', encoding='utf-8') as file_video:
		for line in file_video:
			video_message = line.split(",")
			if int(video_id) == int(video_message[0]):
				name = video_message[1]
				break
	if not name:
		print("查询不到视频id为:{}的视频新闻".format(video_id))
	else:
		print("查询到新闻:{}".format(name))


# 按名字搜索
def search_by_name(name):
	print("您要搜索名称包含{}的新闻".format(name))
	with open(config.VIDEO_PATH, mode='rt', encoding='utf-8') as file_video:
		videos = []
		for line in file_video:
			line_name = "".join(line.split("http")[0].split(",")[1:])
			if name in line_name:
				videos.append(line_name)
		return videos


# 显示数据结果
def show_search_result(video_list):
	print("为你查询到{}条结果".format(len(video_list)))
	for line in video_list:
		print(line)


# 按名字搜索
def search_video():
	while True:
		num = input("输入id或者新闻名称，【q或Q退出】:").strip()
		if num.upper() == "Q":
			break
		if num.isdecimal():
			search_by_id(int(num))
		else:
			show_search_result(search_by_name(num))


